import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_jpf_core/config/app_theme.dart';
import 'package:flutter_jpf_core/model/select_model.dart';
import 'package:flutter_jpf_core/r.dart';
import 'package:flutter_jpf_core/util/colors_util.dart';
import 'package:flutter_jpf_core/util/date_util.dart';
import 'package:flutter_jpf_core/widgets/custom_text.dart';
import 'package:flutter_jpf_core/widgets/date_picker/date_picker.dart';
import 'package:flutter_jpf_core/widgets/select_view.dart';
import './date_picker_mode.dart' as JPF;




class StartEndTimeWidget extends StatefulWidget{
  EdgeInsets _margin;
  Function(DateTime startTime,DateTime endTime) _callSetTime;
  Color _activeBgColor;
  Color bgColor;
  Image iconDate;
  JPF.DatePickerMode datePickerMode;

  StartEndTimeState pageState;

  StartEndTimeWidget(Function(DateTime startTime,DateTime endTime) callSetTime,{
    EdgeInsets margin = const EdgeInsets.only(top: 10,bottom: 10),
    Color activeBgColor,
    this.iconDate,
    this.datePickerMode = JPF.DatePickerMode.YearMonthDay,
    this.bgColor = Colors.transparent
  }){
    this._callSetTime = callSetTime;
    this._margin = margin;
    this._activeBgColor = activeBgColor == null?ColorsUtil.activeBgColor:activeBgColor;
  }

  @override
  State<StatefulWidget> createState() {
    pageState=StartEndTimeState();
    return pageState;
  }
}

enum DateTimeSelType{
  startTimeType,
  endTimeType,
}

class StartEndTimeState extends State<StartEndTimeWidget>{

  List<SelectModel> day7Lst = [
    SelectModel("今天",1,),
    SelectModel("三天内",3),
    SelectModel("七天内",7,isSelect: true),
    SelectModel("自定义",99),
  ];

  DateTime startTime;
  DateTime endTime;

  int dayIndex = 3;

  String dateFormat(){
    JPF.DatePickerMode model = widget.datePickerMode;
    String str = "";
    switch(model){
      case JPF.DatePickerMode.YearMonthDayHourMinuteSecond:
        str="yyyy-MM-dd HH:mm:ss";break;
      case JPF.DatePickerMode.YearMonthDayHourMinute:
        str="yyyy-MM-dd HH:mm";break;
      case JPF.DatePickerMode.YearMonthDay:
        str="yyyy-MM-dd";break;
      case JPF.DatePickerMode.YearMonth:
        str="yyyy-MM";break;
      case JPF.DatePickerMode.MonthDayHourMinute:
        str="MM-dd HH:mm";break;
      case JPF.DatePickerMode.MonthDay:
        str="MM-dd";break;
      case JPF.DatePickerMode.HourMinuteSecond:
        str="HH:mm:ss";break;
      case JPF.DatePickerMode.HourMinute:
        str="HH:mm";break;
    }
    return str;
  }

  @override
  void initState() {
    setState(() {
      DateTime now = DateTime.now();
      startTime = DateUtil.dateBeginTime(now);
      endTime = DateUtil.dateEndTime(now);
    });
    //widget._callSetTime(startTime,endTime);
  }

  void resetData(){
    for (int j = 0; j < day7Lst.length; j++) {
      SelectModel item = day7Lst[j];
      day7Lst[j].isSelect = false;
      if(j == 2){
        day7Lst[j].isSelect = true;

        setStartEndTime(item.val);
      }
    }
    widget._callSetTime(startTime,endTime);
    setState(() {

    });
  }

  @override
  Widget build(BuildContext context) {
    return Container(
      color: widget.bgColor,
      // padding: EdgeInsets.all(15),
      margin: widget._margin,
      child: Column(
        children: [
          Container(
            padding: EdgeInsets.all(15),
            // width: ScreenUtil.screenWidth,
            child: Wrap(
                runSpacing: 6,
                spacing: 10,
                children: day7Lst.map((SelectModel model) => SelectView("${model.name}",
                  radius: 99,
                  activeBgColor: widget._activeBgColor,
                  isSelect: model.isSelect,
                  //constraints: BoxConstraints(minWidth: (ScreenUtil.screenWidth - 60)/4-1,minHeight: 30,maxWidth: (ScreenUtil.screenWidth - 60)/4),
                  onSelectChanged: (bool selected) {
                    model.isSelect = selected;
                    for (int j = 0; j < day7Lst.length; j++) {
                      SelectModel item = day7Lst[j];
                      day7Lst[j].isSelect = false;
                      if(item.val == model.val){
                        day7Lst[j].isSelect = true;

                        setStartEndTime(item.val);
                      }
                    }
                    setState(() {});
                    widget._callSetTime(startTime,endTime);
                  },
                )).toList()),
          ),

          /*Visibility(
              visible: dayIndex == 99,
              child: Column(
                children: [
                  Container(height: 15,color: ColorsUtil.view_background,),
                  Container(
                    padding: EdgeInsets.all( 15),
                    child: GestureDetector(
                    onTap: toSelStartTime,
                    child: Row(
                      crossAxisAlignment: CrossAxisAlignment.end,
                      children: widget.iconDate != null?[
                        Expanded(flex: 1,child: CustomText("开始时间",)),
                        CustomText(strBtime(),margin: EdgeInsets.only(left: 10,right: 10),),
                        widget.iconDate
                      ]:[
                        Expanded(flex: 1,child: CustomText("开始时间",)),
                        CustomText(strBtime(),margin: EdgeInsets.only(left: 10,right: 10),)
                      ],
                    ),
                  ),),
                  // Container(height: 15,),
                  Container(
                    padding: EdgeInsets.only(left: 15,right: 15,bottom: 15),
                    child: GestureDetector(
                onTap: toSelEndTime,
                child: Row(
                  crossAxisAlignment: CrossAxisAlignment.end,
                  children:  widget.iconDate != null?[
                  Expanded(flex: 1,child: CustomText("结束时间")),
                  CustomText(strEtime(),margin: EdgeInsets.only(left: 10,right: 10),),
                  widget.iconDate
                  ]:[
                  Expanded(flex: 1,child: CustomText("结束时间")),
                  CustomText(strEtime(),margin: EdgeInsets.only(left: 10,right: 10),)
                  ],
                ),
              ),
                  )
                ],
              )
          ),*/
        ],
      ),
    );
  }

  String strBtime(){
    return DateUtil.format(startTime,format: dateFormat());
  }
  String strEtime(){
    return DateUtil.format(endTime,format: dateFormat());
  }

  void toSelStartTime(){
    _toSelectDate(DateTimeSelType.startTimeType);
  }

  void toSelEndTime(){
    _toSelectDate(DateTimeSelType.endTimeType);
  }

  String dateStr;
  _setDate(DateTimeSelType type) {
    return (_date) {
      setState(() {
        dateStr = MyDate.format(dateFormat(), _date);
        switch(type){
          case DateTimeSelType.startTimeType:
            startTime = _date;
            break;
          case DateTimeSelType.endTimeType:
            endTime = _date;
            break;
        }
        widget._callSetTime(startTime,endTime);
      });
    };
  }

  void _toSelectDate(DateTimeSelType type){
    DatePickerUtil.showPicker(
      context: context,
      current: dateStr,
      mode: widget.datePickerMode,
      onConfirm: _setDate(type), //yyyy-MM-dd HH:mm:ss
    );
  }

  void setStartEndTime(int index){
    dayIndex = index;
    DateTime now = DateTime.now();
    switch(index){
      case 1:
        startTime = DateUtil.dateBeginTime(now);
        endTime = DateUtil.dateEndTime(now);
        break;
      case 3:
        startTime = DateUtil.dateBeginTime(now,beforeDay: 2);
        endTime = DateUtil.dateEndTime(now);
        break;
      case 7:
        startTime = DateUtil.dateBeginTime(now,beforeDay: 6);
        endTime = DateUtil.dateEndTime(now);
        break;
      case 99:
        break;
    }
  }

  String getDateByAddDay(int day){
    var now = new DateTime.now();
    DateTime time = now.add(new Duration(days: day));
    return DateUtil.format(time,format: "yyyy-MM-dd");
  }
}